package com.open.cloud.rbac.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.inspur.micro.core.response.ResponseWrapper;
import com.inspur.micro.core.utils.RSAUtil;
import com.open.cloud.rbac.object.user.CoreLoginCheckCodeDto;
import com.open.cloud.rbac.object.user.CoreUserChangePwdDto;
import com.open.cloud.rbac.object.user.CoreUserDto;
import com.open.cloud.rbac.object.user.CoreUserLoginInfoDto;
import com.open.cloud.rbac.service.user.ICoreUserLoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户登录信息 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-账户业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/user/login/info")
public class CoreUserLoginInfoController {

    @Resource
    private ICoreUserLoginInfoService userLoginInfoService;

    @ApiOperation(value = "获取Rsa公钥")
    @GetMapping(value = "/publicKey")
    public ResponseWrapper<String> getRsaPublicKey() {
        String publicKeyStr = RSAUtil.getPublicKeyStr();
        return ResponseWrapper.success(publicKeyStr);
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/reset/pwd")
    public ResponseWrapper resetPassword(@RequestBody CoreUserDto userDto) {
        userDto.setOperate("resetPwd");
        JSONObject data = userLoginInfoService.userLoginInfoChange(userDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "账号删除")
    @PostMapping("/delete")
    public ResponseWrapper deleteUserInfo(@RequestBody CoreUserDto userDto) {
        userDto.setOperate("delete");
        JSONObject data = userLoginInfoService.userLoginInfoChange(userDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/change/pwd")
    public ResponseWrapper changeUserPassword(@RequestBody CoreUserChangePwdDto changePwdDto) {
        JSONObject data = userLoginInfoService.changeLoginPassword(changePwdDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "缓存验证码")
    @PostMapping("/check/code")
    public ResponseWrapper saveLoginCheckCode(@RequestBody CoreLoginCheckCodeDto checkCodeDto) {
        JSONObject data = userLoginInfoService.saveCheckCode(checkCodeDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public ResponseWrapper userLogin(@RequestBody CoreUserLoginInfoDto userLoginInfoDto) {
        JSONObject result = userLoginInfoService.checkLoginInfo(userLoginInfoDto);
        JSONObject data = result.getJSONObject("data");
        String message = result.getString("message");
        Integer code = result.getInteger("code");
        return code == 200 ? ResponseWrapper.success(data) : ResponseWrapper.failure(code, message);
    }

}

